package me.leiusual.sharing.ui.mine

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Transformations
import androidx.lifecycle.ViewModel
import me.leiusual.sharing.api.model.RealName
import me.leiusual.sharing.api.model.RealNameQuery
import me.leiusual.sharing.api.model.SendSMS
import me.leiusual.sharing.pref.User
import me.leiusual.sharing.repository.UserRepository
import me.leiusual.sharing.repository.model.Data
import me.leiusual.sharing.ui.base.AbstractViewModel
import me.leiusual.sharing.ui.mine.model.ParamsRealName
import me.leiusual.sharing.util.extension.toLiveData
import javax.inject.Inject

/**
 * Create by leiusual on 2018/7/16
 */
class MineAuthViewModel @Inject constructor(
    private val userRepository: UserRepository,
    val user: User
) : ViewModel() {

  private val _realNameQuery = MutableLiveData<Unit>()
  val realNameQuery: LiveData<Data<RealNameQuery.Response>> = Transformations.switchMap(_realNameQuery) {
    userRepository.realNameQuery().toLiveData()
  }

  fun realNameQuery() {
    _realNameQuery.value = Unit
  }

  private val _sendSMS = MutableLiveData<String>()
  val sendSMS: LiveData<Data<Unit>> = Transformations.switchMap(_sendSMS) {
    userRepository.sendSMS(SendSMS.Type.REAL_NAME_AUTH, it).toLiveData()
  }

  fun sendSMS(mobile: String?) {
    _sendSMS.value = mobile ?: return
  }

  private val _filePathFront = MutableLiveData<String>()
  val filePathFront: LiveData<String> = _filePathFront

  fun setFilePathFront(filePathFront: String) {
    _filePathFront.value = filePathFront
  }

  private val _filePathBack = MutableLiveData<String>()
  val filePathBack: LiveData<String> = _filePathBack

  fun setFilePathBack(filePathBack: String) {
    _filePathBack.value = filePathBack
  }

  private val _realName = MutableLiveData<ParamsRealName>()
  val realName: LiveData<Data<Unit>> = Transformations.switchMap(_realName) {
    userRepository.realName(it).toLiveData()
  }

  fun realName(realName: String, idCard: String, checkCode: String) {
    val front = filePathFront.value
    val back = filePathBack.value
    _realName.value = ParamsRealName(realName, idCard, RealName.Type.ID_CARD_2, checkCode, front, back)
  }
}